//
//  KeychainViewController.swift
//  Entrance
//
//  Created by chunhuiLai on 2018/1/11.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import UIKit
import SnapKit
import SYKit
import KeychainAccess

class KeychainViewController: UIViewController {
    
    lazy var vendorLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    lazy var uuidLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        layoutUI()
        
        loadData()
    }

    func loadData() {
        let vendor = SYApplicationInfo.idfvString() ?? "未知"
        
        vendorLabel.sy_SetColor([UIColor.purple, UIColor.orange], strArr: ["idfv: ", vendor])
        
        let keychain = Keychain(service: Bundle.main.bundleIdentifier ?? "")
        
        if let uuid = keychain["vendor"] {
            uuidLabel.sy_SetColor([UIColor.purple, UIColor.orange], strArr: ["vendor: ", uuid])
        }
        else {
            uuidLabel.sy_SetColor([UIColor.purple, UIColor.orange], strArr: ["vendor: \n", "未存储"])
            try? keychain.set(vendor, key: "vendor")
        }
    }
    
    func layoutUI() {
        view.addSubview(vendorLabel)
        view.addSubview(uuidLabel)
        
        vendorLabel.snp.makeConstraints { (make) in
            make.top.equalTo(150)
            make.left.equalTo(16)
        }
        
        uuidLabel.snp.makeConstraints { (make) in
            make.top.equalTo(vendorLabel.snp.bottom).offset(35)
            make.left.equalTo(16)
        }
    }
}
